<?php
/**
 * Author:   tangxiaowen
 * Email:    846506584@qq.com
 * Date:     2019/3/12 0012
 * Describe: 协议管理类
 */

namespace app\index\controller;

class Agreement extends Base
{

    /**
     * 获取协议内容
     * @param integer $type 协议类型
     * @return array
     */
    public function getAgreement()
    {
        //验证post数据
        $postData = Request()->only('type,userId,orderId');
        $result = $this->validate($postData,'Agreement.getAgreement');
        if($result !== true){
            return json(['status' => '-1','msg' => $result,'data' => []]);
        }

        $post = Request()->header();

        //判断需要协议内容
        switch ($postData['type']){
            case 1:
                $str = '注册协议';
                $agreementData = $this->getWithhold($str);
                break;
            case 2:
                $str = '借款协议';
                $agreementData = $this->getWithhold($str,isset($post['userid'])?$post['userid']:'',isset($postData['orderId'])?$postData['orderId']:'');
                break;
            case 3:
                $str = '展期协议';
                $agreementData = $this->getWithhold($str,isset($post['userid'])?$post['userid']:'',isset($postData['orderId'])?$postData['orderId']:'');
                break;
            case 4:
                $str = '代扣协议';
                $agreementData = $this->getWithhold($str);
                break;
            default:
                $agreementData = false;
        }

        if($agreementData){
            returnSuccessResult($agreementData);
        }else{
            returnErrorResult();
        }
    }

    /**
     * 关于我们
     * @return string
     */
    public function getWeInfo()
    {
        $weInfo = db('sys_content_change')->where(['Title' => '关于我们'])->value('Contents');
        if($weInfo){
            returnSuccessResult($weInfo);
        }else{
            returnErrorResult('未获取协议信息','','');
        }
    }

    /**
     * 获取代扣协议
     * @param string $str 协议名称
     * @param int $userid 用户ID
     * @param int $orderId 订单ID
     * @return array
     */
    private function getWithhold($str,$userId = '',$orderId = ''){

        //获取系统配置信息及协议内容
        $systemName = (new System())->getSystemInfo();

        //获取协议内容数据并替换内容数据
        $agreementData = db('sys_content_change')->where(['Title' => $str])->value('Contents');

        //注册协议
        if($str == '注册协议'){
            return $agreementData;
        }

        //代扣协议替换公司名称
        if($str == '代扣协议'){
            return str_replace('某网络信息科技有限公司',$systemName['SystemName'],$agreementData);
        }

        //获取用户信息
        $userInfo = (new Member())->getUserInfo($userId);
        $arr1 = ['甲方名称',            '甲方身份证',         '签订日期'];
        $arr2 = [$userInfo['TrueName'], $userInfo['IDCard'], date('Y年m月d日')];

        //借款协议替换借款人数据
        if($str == '借款协议' && $orderId){
            $orderInfo = (new Order())->getOrderInfo($orderId);
            array_push($arr1, '订单编号',               '乙方名称',            '乙方身份证',                  '丙方名称',                '丁方名称',              '1000.00',               '2018年12月19日',       '2018年12月25日',           '36%',                         '[7]天',                          '借款还款日',             '1007.00');
            array_push($arr2,  $orderInfo['OrderSn'],  $systemName['yifang'], $systemName['shenfenzheng'],  $systemName['bingfang'], $systemName['dingfang'], $orderInfo['AdoptMoney'], $orderInfo['OpenTime'], $orderInfo['YyFkTime'], $orderInfo['nianhualilv'].'%', '['.$orderInfo['ApplyDay'].']天', $orderInfo['YyFkTime'],  $orderInfo['ApplyMoney']);
        }

        //展期协议替换公司名称等
        if($str == '展期协议' && $orderId){
            $orderInfo = (new Order())->getOrderInfo($orderId);
            array_push($arr1,'乙方名称',            '1000.00',                '订单编号',              '2018年12月19日',        '2018年12月25日',       '36%',                         '[7]天',                          '1007.00');
            array_push($arr2,$systemName['yifang'], $orderInfo['AdoptMoney'],  $orderInfo['OrderSn'], $orderInfo['OpenTime'], $orderInfo['YyFkTime'], $orderInfo['nianhualilv'].'%', '['.$orderInfo['ApplyDay'].']天', $orderInfo['ApplyMoney']);
        }

        return str_replace($arr1,$arr2,$agreementData);
    }


}